import unittest
import sys
import os

sys.path += [os.path.abspath('..')]

from utils.video_thumbnailer import VideoThumbnailer, VideoThumbnailerException
from utils import thumbnailer

class VideoThumbnailerTest(unittest.TestCase):

    def setUp(self):
        self.debug = False

    def testThumbnailerConstructor(self):
        '''testThumbnailConstructor - Ensures instantiation of thumbnailer
        class'''
        self.thumbnailer = VideoThumbnailer(os.path.abspath('.') + '/data/VideoThumbnailer/test.avi', src='video')
        self.assertTrue(isinstance(self.thumbnailer, VideoThumbnailer))

    def testThumbnailerConstructorFilenameIsFolder(self):
        '''testThumbnailerConstructorFilenameIsFolder - Ensures that VideoThumbnailer
        can handle a folder being passed as the filename instead of an actual
        file'''
        self.assertRaises(thumbnailer.ThumbnailerException, VideoThumbnailer, os.path.abspath('.'))

    def testThumbnailerConstructorFilenameExists(self):
        '''testThumbnailerConstructorFilenameExists - Ensures the filename to
        thumbnail actually exists'''
        self.assertRaises(thumbnailer.ThumbnailerException, VideoThumbnailer, os.path.abspath('foo-bar-baz'))
    
    def testThumbnailerConstructorSrc(self):
        '''testThumbnailerConstructorSrc - Ensures that you can't pass any src
        types other than those that are valid'''
        self.assertRaises(thumbnailer.ThumbnailerException, VideoThumbnailer, os.path.abspath('.') + '/data/VideoThumbnailer/test.avi', src="foo")
    
    def testThumbnailAvi(self):
        '''testThumbnailAvi - Tests the VideoThumbnailer.create_thumbnail()
        function on data/test.avi'''
        thumbnailer = VideoThumbnailer(os.path.abspath('.') + '/data/VideoThumbnailer/test.avi', src='video')
        if os.path.exists(thumbnailer._thumb_file):
            os.remove(thumbnailer._thumb_file)
        thumbnailer.create_thumbnail()
        if self.debug:
            print 'Expecting thumbnail : %s' % (self.thumbnailer._thumb_file)
        self.assertTrue(os.path.exists(thumbnailer._thumb_file))
        
    def testThumbnailFlv(self):
        '''testThumbnailFlv - Tests the VideoThumbnailer.create_thumbnail()
        function on data/test.flv'''
        thumbnailer = VideoThumbnailer(os.path.abspath('.') + '/data/VideoThumbnailer/test.avi', src='video')
        if os.path.exists(thumbnailer._thumb_file):
            os.remove(thumbnailer._thumb_file)
        thumbnailer.create_thumbnail()
        if self.debug:
            print 'Expecting thumbnail : %s' % (self.thumbnailer._thumb_file)
        self.assertTrue(os.path.exists(thumbnailer._thumb_file))
